package web_filter

import (
	"gitee.com/kristas/booting-go/framework/web"
	"net/http"
)

type FilterChain struct {
	filter  web.Filter
	next    web.FilterChain
	service web.Service
}

func (f *FilterChain) GetFilter() web.Filter {
	return f.filter
}

func (f *FilterChain) SetNext(chain web.FilterChain) {
	f.next = chain
}

func (f *FilterChain) GetNext() web.FilterChain {
	return f.next
}

func (f *FilterChain) SetService(service web.Service) {
	f.service = service
}

func (f *FilterChain) DoService() error {
	return f.service()
}

func (f *FilterChain) DoFilter(w http.ResponseWriter, r *http.Request) error {
	next := f.GetNext()
	if next == nil {
		return f.DoService()
	}
	return next.GetFilter().DoFilter(w, r, next)
}

func newFilterChain(filter web.Filter, service web.Service) web.FilterChain {
	return &FilterChain{
		filter:  filter,
		service: service,
	}
}
